package jgo.plugins.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.sun.net.httpserver.HttpExchange;

import jgo.Context;
import jgo.User;

public class HttpContext extends Context {
	public HttpExchange exchange;
	public int statusCode = 200;
	public boolean responseSent = false;
	public User user;
	public Map<String, String> headers = new HashMap<>();
	public Map<String, String> cookies = new HashMap<>();

	// ... 其他现有方法 ...
	public HttpContext() {
	}

	public HttpContext(HttpExchange exchange) {
		this.exchange = exchange;
	}

	// 你可能需要添加一个方法来设置cookies，比如：
	public void setCookie(String name, String value) {
		cookies.put(name, value);
	}

	@Override
	public String getMethod() {
		return exchange.getRequestMethod();
	}

	@Override
	public String getPath() {
		return exchange.getRequestURI().getPath();
	}

	@Override
	public Context status(int statusCode) {
		this.statusCode = statusCode;
		return this;
	}

	@Override
	public Context json(Object obj) {
		try {
			String jsonResponse = JSON.toJSONString(obj);
			byte[] responseBytes = jsonResponse.getBytes(StandardCharsets.UTF_8);
			exchange.getResponseHeaders().set("Content-Type", "application/json");
			exchange.sendResponseHeaders(statusCode, responseBytes.length);
			try (OutputStream os = exchange.getResponseBody()) {
				os.write(responseBytes);
			}
			responseSent = true;
		} catch (IOException e) {
			throw new RuntimeException("Error sending JSON response", e);
		}
		return this;
	}

	@Override
	public Context string(String string) {
		

		try {
			byte[] responseBytes = string.getBytes(StandardCharsets.UTF_8);
			exchange.getResponseHeaders().set("Content-Type", "application/json");
			exchange.sendResponseHeaders(statusCode, responseBytes.length);
			try (OutputStream os = exchange.getResponseBody()) {
				os.write(responseBytes);
			}
			responseSent = true;
		} catch (IOException e) {
			throw new RuntimeException("Error sending JSON response", e);
		}
		return this;
	
		
		
	}
	public Context header(String name, String value) {
		exchange.getResponseHeaders().set(name, value);
		return this;
	}

	public void end() {
		if (!responseSent) {
			try {
				exchange.sendResponseHeaders(statusCode, -1);
			} catch (IOException e) {
				throw new RuntimeException("Error ending response", e);
			}
			responseSent = true;
		}
		exchange.close();
	}

	@Override
	public String body() {
		try (InputStream inputStream = exchange.getRequestBody();
				ByteArrayOutputStream result = new ByteArrayOutputStream()) {
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inputStream.read(buffer)) != -1) {
				result.write(buffer, 0, length);
			}
			return result.toString(StandardCharsets.UTF_8.name());
		} catch (IOException e) {
			throw new RuntimeException("Error reading request body", e);
		}
	}

	@Override
	public String getHeader(String string) {
		return exchange.getRequestHeaders().getFirst(string);
	}

	public String getCookie(String sessionKey) {
		return null;
	}
}